package com.example.myapplication.model;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.annotation.NonNull;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.example.myapplication.R;

/**
 * @author: rongfeng
 * @date: 2020/11/23
 * @description PK积分条动效
 */
public class PKScoreAnimator {
   private ImageView view_attack;
    private long lastLeftScore, lastRightScore;

    public PKScoreAnimator(ImageView view_attack) {
        this.view_attack = view_attack;
    }


    /**
     * @param leftScore  左边分数
     * @param rightScore 右边分数
     * @param barColor   当前直播间红蓝方 1=红方 2=蓝方
     * @param state      pk状态 1=pk中 2=pk结束
     */
    public void startScoreBarAnimator(long leftScore, long rightScore, int barColor, int state) {
        boolean isLeftAttack = false;
        boolean isRightAttack = false;
        if (leftScore > lastLeftScore && leftScore > rightScore) { //左边进攻
            isLeftAttack = true;
        } else if (rightScore > lastRightScore && rightScore > leftScore) {//右边进攻
            isRightAttack = true;
        }
        lastLeftScore = leftScore;
        lastRightScore = rightScore;

        int resId = 0;
        boolean bigScore = false;
        if (state == 1) {
            bigScore = Math.abs(leftScore - rightScore) >= 10000;
            if (barColor == 1) {//红色
                if (isLeftAttack) {
                    if (bigScore) {
                        resId = R.mipmap.pk_6;
                    } else {
                        resId = R.mipmap.pk_4;
                    }
                } else if (isRightAttack) {
                    if (bigScore) {
                        resId = R.mipmap.pk_5;
                    } else {
                        resId = R.mipmap.pk_1;
                    }
                }
            } else {// 蓝色
                if (isLeftAttack) {
                    if (bigScore) {
                        resId = R.mipmap.pk_6;
                    } else {
                        resId = R.mipmap.pk_2;
                    }
                } else if (isRightAttack) {
                    if (bigScore) {
                        resId = R.mipmap.pk_5;
                    } else {
                        resId = R.mipmap.pk_3;
                    }
                }
            }
        } else if (state == 2) {
            if (leftScore > rightScore) {
                resId = R.mipmap.pk_7;
            } else if (leftScore == rightScore) {
                resId = R.mipmap.pk_8;
            } else {
                resId = R.mipmap.pk_9;
            }
        }

        if (resId != 0) {
            view_attack.setImageResource(resId);
        }

        if (isLeftAttack || isRightAttack) {
            mHandler.removeMessages(1);
            if (!bigScore) {
                mHandler.sendEmptyMessageDelayed(1, 3000);
            }
        }

    }

    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                if (view_attack != null) {
                    view_attack.setImageResource(0);
                }
            }
        }
    };

    public void clear() {
        if (mHandler != null) {
            mHandler.removeMessages(1);
        }
        lastLeftScore = 0;
        lastRightScore = 0;
    }
}
